package pl.edu.mimuw.mas.agent.behaviour.hunting;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import pl.edu.mimuw.mas.agent.HuntingAgent;
import pl.edu.mimuw.mas.agent.behaviour.ServerBehaviour;
import pl.edu.mimuw.mas.message.PositionMessage;
import pl.edu.mimuw.mas.util.Position;

public class DeerPositionServerBehaviour extends ServerBehaviour {

	private static final long serialVersionUID = 6786893614272367927L;
	
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	public DeerPositionServerBehaviour(HuntingAgent agent) {
		super(agent);
		
		MessageTemplate mt = MessageTemplate.and(
				MessageTemplate.MatchConversationId(HuntingAgent.DEER_POSITION_INFO_CONV),
				MessageTemplate.MatchPerformative(ACLMessage.INFORM)
				);
		setMessageTemplate(mt);
	}

	public boolean doAction(ACLMessage msg, ACLMessage reply) throws UnreadableException {
		// Deserializacja SL
		PositionMessage pm = (PositionMessage) msg.getContentObject();
		
		if (pm != null) {
			HuntingAgent a = (HuntingAgent) myAgent;
			a.setDeerPosition(new Position(pm.x, pm.y));
			a.setLastDeerPositionUpdate(System.currentTimeMillis());
			a.scare();
		}
		
		return false;
	}

}
